package com.mingchuang.administrator.mingchuang.zip;

import android.content.Context;
import android.util.Log;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.table.DbModel;
import com.lidroid.xutils.exception.DbException;
import com.mingchuang.administrator.mingchuang.AppApplication;
import com.mingchuang.administrator.mingchuang.bean.PileInfoBean;
import com.mingchuang.administrator.mingchuang.bean.ProjectBean;
import com.mingchuang.administrator.mingchuang.utils.Constant;
import com.mingchuang.administrator.mingchuang.xml.PileXml;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/21.
 */
public class FileUtils {

    /**
     * 读取解压文件  讲相关信息写入数据库
     * @param path 解压文件路径
     */

    public static void read(Context context ,String path ){
        File file = new File(path);
        String progectName = file.getName();

        File[] files = file.listFiles();
        List<File> pileList = new ArrayList<File>();

        for (File f :files){

            if(f.isDirectory()){
                //基桩数据
                Log.d("kipeng","pile name:"+f.getName());
                pileList.add(f);

            }
        }

        //存储工程信息
        int jizhaungNum = pileList.size();
        int unUpNum = pileList.size();

        DbUtils db = AppApplication.getDb();
        ProjectBean projectBean = new ProjectBean();
        projectBean.setName(progectName);
        projectBean.setJizhuangNum(jizhaungNum);
        projectBean.setUnLoadNum(unUpNum);
        projectBean.setPath(path);
        try {
            db.save(projectBean);
        } catch (DbException e) {
            e.printStackTrace();
        }

        savePileInfo(pileList , context);


    }

    /**
     * 存储基桩信息  读取xml信息
     * @param pileList
     */
    private static void savePileInfo(List<File> pileList ,Context context) {
        for (File file : pileList){

            File[] piles = file.listFiles();
            for(File xmlFile : piles){
               Log.d("kipeng","pile name:"+xmlFile.getName());
                String pileName = xmlFile.getName();
                if(pileName.contains("xml") && xmlFile.isFile()){
                    //读取xml文件
                    PileInfoBean pile = PileXml.readXML(xmlFile);
                    pile.setPath(file.getPath());
                    DbUtils db = AppApplication.getDb();
                    try {
                        db.save(pile);
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

    }

}
